//
//  ShowAllVC.swift
//  mmda
//
//  Created by 李陆涛 on 2018/8/6.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit

class ShowAllVC: BaseVC {

    @IBOutlet weak var tagView: UIView!
    @IBOutlet weak var tagViewLeading: NSLayoutConstraint!
    
    @IBOutlet weak var headViewTop: NSLayoutConstraint!
    @IBOutlet weak var blogBtn: UIButton!
    @IBOutlet weak var userBtn: UIButton!
    
    @IBOutlet weak var contentView: UIView!
    
    private var pageVC: UIPageViewController!
    
    private var showType: ShowType = .blog
    
    private var pageArray:[UIViewController]?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "全部内容"
        setupUI()
    }
    
    private func setupUI() {
        
        headViewTop.constant = CGFloat(kNavigationBarHeight);
        
        let blogVC = BlogOrUserListVC(pageType: .allblog)
        let userVC = BlogOrUserListVC(pageType:.alluser)
        
        pageArray = [blogVC,userVC]
        
        pageVC = UIPageViewController(transitionStyle: UIPageViewControllerTransitionStyle.scroll, navigationOrientation: UIPageViewControllerNavigationOrientation.horizontal, options: nil)
        
        pageVC.setViewControllers([blogVC], direction: .forward, animated: true, completion: nil)

        self.addChildViewController(pageVC)
        self.contentView.addSubview(pageVC.view)
        
//        pageVC.view.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight - 44)
        
        pageVC.view.snp.makeConstraints {
            
            [weak self] maker in
            guard let `self` = self else { return }
            
            maker.edges.equalTo(self.contentView)
        }
        
        pageVC.delegate = self
        pageVC.dataSource = self
    }
    
    @IBAction func showBlogAction(_ sender: UIButton) {
        
        showType = .blog
        
        makeTagViewAnimation()
        changeTab()
    }
    
    @IBAction func showUserAction(_ sender: UIButton) {
        
        showType = .user
        
        makeTagViewAnimation()
        changeTab()
    }
    
    //    动画
    func makeTagViewAnimation() {
        
        let constant = showType == ShowType.user ? CGFloat(kScreenWidth) / 2.0 : 0
        
        self.tagViewLeading.constant = constant
        
        UIView.animate(withDuration: 0.25) {
            [weak self] in
            guard let `self` = self else { return }
            
            self.view.layoutIfNeeded()
        }
    }
    
    func changeTab() {
        
        guard let vc = (showType == ShowType.user) ? pageArray?.last : pageArray?.first else { return }
        
        pageVC.setViewControllers([vc], direction: (showType == ShowType.user) ? .forward : .reverse, animated: true, completion: nil)
    }
}

extension ShowAllVC : UIPageViewControllerDataSource,UIPageViewControllerDelegate {
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        
        var index = pageArray?.index(of: viewController)
        
        if index == 0 || index == NSNotFound {
            
            return nil
        }
        
        index = index! - 1
        
        return pageArray?[index!]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        
        var index = pageArray?.index(of: viewController)
        
        if index == NSNotFound || index == ((pageArray?.count)! - 1) {
            
            return nil
        }
        
        index = index! + 1
        
        return pageArray?[index!]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        
        
        if !completed {
            return
        }

        guard let vc = previousViewControllers.first else { return }

        let index = self.pageArray?.index(of: vc)

        showType = index == 0 ? .user : .blog

        makeTagViewAnimation()
    }
}
